<%@ Page debug="true" Trace="false" %>

<%@ Import Namespace="System.Diagnostics" %>

<html>
<head>
<!--------------------------------------------------------------------------->

<html>

<script language="VB" runat="server">

  Dim Category As String = "Wrox"
  Dim Counter  As String = "Web Services"
  

  Sub Page_Load(Sender As Object, e as EventArgs)

    If Not Page.IsPostBack Then
      If PerformanceCounterCategory.Exists(Category) Then
        AlreadyExists.Visible = True
      End If
    End If

  End Sub
  

  Sub CreateCounters_Click(Sender As Object, e as EventArgs)
  
    ' only create the counters if they don't already exist
    If Not PerformanceCounterCategory.Exists(Category) Then
      ' set up the counter details
      Dim ccdc As New CounterCreationDataCollection()

      ccdc.Add(New CounterCreationData(Counter, "Web Service Requests", PerformanceCounterType.NumberOfItems32))

      PerformanceCounterCategory.Create(Category, "Wrox Press Professional ASP.NET Samples", ccdc)
    End If

    ShowCounterValues()
    
  End Sub


  Sub ProcessCounter_Click(Sender As Object, e as CommandEventArgs)

    If PerformanceCounterCategory.Exists(Category) Then
    
      ' CommandArgument contains the name of the counter
      Dim pc As New PerformanceCounter(Category, Counter, e.CommandArgument, False)
      pc.Increment()

      pc.InstanceName = "_Total"
      pc.Increment

      ShowCounterValues()
    End If

  End Sub
  
  
  Sub DeleteCounters_Click(Sender As Object, e as EventArgs)
  
    ' delete all counters and the catageory
    If PerformanceCounterCategory.Exists(Category) Then
      PerformanceCounterCategory.Delete(Category)
      Page.Controls.Remove(CounterValues)
    End If
  
  End Sub


  ' create a table showing the counter values
  Sub ShowCounterValues()
  
    If PerformanceCounterCategory.Exists(Category) Then
      Dim cat As New PerformanceCounterCategory(Category)
      Dim pc  As New PerformanceCounter
      Dim s   As String
      Dim tr  As TableRow
      Dim tcc As TableCell
      Dim tcv As TableCell
      Dim tct As TableCell
      Dim img As HtmlImage

      ' Construct a table row for each counter
      For Each pc In cat.GetCounters()
        ' cell for text
        tcc = New TableCell
        tcc.Text = pc.CounterHelp
        tcc.Width = Unit.Percentage(20)
        
        ' text value
        tct = New TableCell
        tct.Text = pc.rawValue
        tct.Width = Unit.Percentage(5)
        
        ' bar graph and cell containing it
        img = New HtmlImage
        img.Src = "RedPixel.gif"
        img.Width = pc.RawValue
        img.Height = 20
        tcv = New TableCell
        tcv.Controls.Add(img)
        
        ' add cells to row, and rows to table
        tr = New TableRow
        tr.Cells.Add(tcc)
        tr.Cells.Add(tct)
        tr.Cells.Add(tcv)
        CounterValues.Rows.Add(tr)
      Next
    End If

  End Sub

</script>

<form runat="server">

  <asp:Button Text="Create Counters" onclick="CreateCounters_Click" runat="server"/>
  &nbsp;
  <asp:Button Text="Delete Counters" onclick="DeleteCounters_Click" runat="server"/>
  <p/>
  
  <table cellspacing="10">
    <tr>
      <td align="center">Instance 1</td>
      <td align="center">Instance 2</td>
      <td align="center">Instance 3</td>
    </tr>
    <tr>
      <td>
        <asp:Button CommandArgument="Instance1" Text="Increment"
             onCommand="ProcessCounter_Click" runat="server"/>
      </td>
      <td>
        <asp:Button CommandArgument="Instance2" Text="Increment"
             onCommand="ProcessCounter_Click" runat="server"/>
      </td>
    </tr>
  </table>
        
  <p/>

  <asp:Table id="CounterValues" width="75%" runat="server">
    <asp:TableRow>
      <asp:TableHeaderCell HorizontalAlign="Left">Counter</asp:TableHeaderCell>
      <asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
      <asp:TableHeaderCell HorizontalAlign="Left">Value</asp:TableHeaderCell>
    </asp:TableRow>
  </asp:table>
  
  <asp:Label id="AlreadyExists"
       Text="The category 'Wrox' already exists. You should delete it before continuing."
       visible = "false" runat="server"/>
</form>

<!--------------------------------------------------------------------------->

